package org.platform.lwc.redis.factory.lettuce;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

import java.util.stream.Collectors;

/**
 * @Description: 集群redis
 * @Author: lwc
 * @Date: 2021/5/20
 */
public class ClusterObtainLettuceConnection implements ObtainLettuceConnection {
    @Override
    public LettuceConnectionFactory obtainLettuceConnection(RedisProperties redisProperties) {
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(cluster.getNodes().stream().collect(Collectors.toSet()));
        redisClusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));


        LettucePoolingClientConfiguration lettuceClientConfiguration = getLettucePoolingClientConfiguration(redisProperties);
        return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
    }
}
